﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;
using System.Web.UI.HtmlControls;
using System.Text;

namespace Web.Page
{
    public partial class ZL_WL_Edit : BasePage
    {
        protected const string GYS = "gys";

        // 下拉框数组
        protected List<DropDownList> listDropDownList;
        // HtmlRow数组
        protected static List<TableRowItem> listItems = new List<TableRowItem>();
        protected List<Label> listLabel;
        protected List<HtmlTableRow> listTableRow;

        ZL_WL_Dao dao = new ZL_WL_Dao();
        protected void Page_Load(object sender, EventArgs e)
        {
            InitAllList();

            if (!IsPostBack)
            {
                this.lblTitle.Text = "修改物料";
                initValue();
            }
        }

        private void InitAllList()
        {
            listDropDownList = new List<DropDownList>();
            listDropDownList.Add(DropDownList1);
            listDropDownList.Add(DropDownList2);
            listDropDownList.Add(DropDownList3);
            listDropDownList.Add(DropDownList4);
            listDropDownList.Add(DropDownList5);
            listDropDownList.Add(DropDownList6);
            listDropDownList.Add(DropDownList7);
            listDropDownList.Add(DropDownList8);

            listLabel = new List<Label>();
            listLabel.Add(Label1);
            listLabel.Add(Label2);
            listLabel.Add(Label3);
            listLabel.Add(Label4);
            listLabel.Add(Label5);
            listLabel.Add(Label6);
            listLabel.Add(Label7);
            listLabel.Add(Label8);

            listTableRow = new List<HtmlTableRow>();
            listTableRow.Add(tr1);
            listTableRow.Add(tr2);
            listTableRow.Add(tr3);
            listTableRow.Add(tr4);
            listTableRow.Add(tr5);
            listTableRow.Add(tr6);
            listTableRow.Add(tr7);
            listTableRow.Add(tr8);

        }


        private void initValue()
        {
            string id = Request.QueryString["id"];
            this.hdId.Value = id;

            ZL_WL wl = dao.Query(id);

            CS_WLLX_Dao daoWLLX = new CS_WLLX_Dao();
            List<CS_WLLX> lsWLLX = daoWLLX.QueryAll();
            this.ddlLXBH.DataSource = lsWLLX;
            this.ddlLXBH.DataTextField = "MC";
            this.ddlLXBH.DataValueField = "BH";
            this.ddlLXBH.DataBind();
            this.ddlLXBH.Items.Insert(0, new ListItem("请选择", "0"));
            this.ddlLXBH.Items.FindByValue(wl.LXBH).Selected = true;

            CS_JBDW_Dao daoJBDW = new CS_JBDW_Dao();
            List<CS_JBDW> lsJBDW = daoJBDW.Query4WL();
            this.ddlDWBH.DataSource = lsJBDW;
            this.ddlDWBH.DataValueField = "BH";
            this.ddlDWBH.DataTextField = "MC";
            this.ddlDWBH.DataBind();
            this.ddlDWBH.Items.Insert(0, new ListItem("请选择", "0"));
            ListItem li = this.ddlDWBH.Items.FindByValue(wl.DWBH);
            if (li != null)
            {
                li.Selected = true;
            }

            CS_YSLX_Dao daoYSLX = new CS_YSLX_Dao();
            List<CS_YSLX> lsYSLX = daoYSLX.QueryAll();
            this.ddlYSLXBH.DataSource = lsYSLX;
            this.ddlYSLXBH.DataTextField = "MC";
            this.ddlYSLXBH.DataValueField = "BH";
            this.ddlYSLXBH.DataBind();
            this.ddlYSLXBH.Items.Insert(0, new ListItem("请选择", "0"));
            ListItem li2 = this.ddlYSLXBH.Items.FindByValue(wl.YSLXBH);
            if (li2 != null)
            {
                li2.Selected = true;
            }

            InitYS();
            ListItem li3 = this.ddlYSBH.Items.FindByValue(wl.YSBH);
            if (li3 != null)
            {
                li3.Selected = true;
            }

            ZL_GYS_Dao daoGYS = new ZL_GYS_Dao();
            List<ZL_GYS> lsGYS = daoGYS.QueryAll();
            Hashtable ht = new Hashtable();
            foreach (ZL_GYS gys in lsGYS)
            {
                ht.Add(gys.ID.ToString(), gys.BH);
            }
            Cache.Insert(GYS, ht);

            this.ddlGYSID.DataSource = lsGYS;
            this.ddlGYSID.DataValueField = "ID";
            this.ddlGYSID.DataTextField = "MC";
            this.ddlGYSID.DataBind();
            this.ddlGYSID.Items.Insert(0, new ListItem("请选择", "0"));
            this.ddlGYSID.Items.FindByValue(wl.GYSID.ToString()).Selected = true;

            this.tbBH.Text = wl.BH;
            this.tbGG.Text = wl.GG;
            this.tbMC.Text = wl.MC;
            this.hdBh.Value = wl.BH;
            this.tbAQKCSL.Text = wl.AQKCSL.ToString();

            // 初始化属性
            ShowSXDropDownList(wl.LXBH);
            if (wl.SXMC1 != null && !wl.SXMC1.Trim().Equals("0"))
            {
                Label1.Text = wl.SXMC1;
            }
            if (wl.SXZBH1 != null && !wl.SXZBH1.Trim().Equals("0"))
            {
                DropDownList1.Items.FindByValue(wl.SXZBH1).Selected = true;
            }
            if (wl.SXMC2 != null && !wl.SXMC2.Trim().Equals("0"))
            {
                Label2.Text = wl.SXMC2;
            }
            if (wl.SXZBH2 != null && !wl.SXZBH2.Trim().Equals("0"))
            {
                DropDownList2.Items.FindByValue(wl.SXZBH2).Selected = true;
            }
            if (wl.SXMC3 != null && !wl.SXMC3.Trim().Equals("0"))
            {
                Label4.Text = wl.SXMC4;
            }
            if (wl.SXZBH3 != null && !wl.SXZBH3.Trim().Equals("0"))
            {
                DropDownList3.Items.FindByValue(wl.SXZBH3).Selected = true;
            }
            if (wl.SXMC4 != null && !wl.SXMC4.Trim().Equals("0"))
            {
                Label4.Text = wl.SXMC4;
            }
            if (wl.SXZBH4 != null && !wl.SXZBH4.Trim().Equals("0"))
            {
                DropDownList4.Items.FindByValue(wl.SXZBH4).Selected = true;
            }
            if (wl.SXMC5 != null && !wl.SXMC5.Trim().Equals("0"))
            {
                Label5.Text = wl.SXMC5;
            }
            if (wl.SXZBH5 != null && !wl.SXZBH5.Trim().Equals("0"))
            {
                DropDownList5.Items.FindByValue(wl.SXZBH5).Selected = true;
            }
            if (wl.SXMC6 != null && !wl.SXMC6.Trim().Equals("0"))
            {
                Label6.Text = wl.SXMC6;
            }
            if (wl.SXZBH6 != null && !wl.SXZBH6.Trim().Equals("0"))
            {
                DropDownList6.Items.FindByValue(wl.SXZBH6).Selected = true;
            }
            if (wl.SXMC7 != null && !wl.SXMC7.Trim().Equals("0"))
            {
                Label7.Text = wl.SXMC7;
            }
            if (wl.SXZBH7 != null && !wl.SXZBH7.Trim().Equals("0"))
            {
                DropDownList7.Items.FindByValue(wl.SXZBH7).Selected = true;
            }
            if (wl.SXMC8 != null && !wl.SXMC8.Trim().Equals("0"))
            {
                Label1.Text = wl.SXMC1;
            }
            if (wl.SXZBH8 != null && !wl.SXZBH8.Trim().Equals("0"))
            {
                DropDownList8.Items.FindByValue(wl.SXZBH8).Selected = true;
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ClearControlsValue();
        }

        /// <summary>
        /// 清空所有TextBox内容
        /// </summary>
        private void ClearControlsValue()
        {
            this.lbResult.Text = "";
            this.tbBH.Text = "";
            this.tbMC.Text = "";
            this.ddlDWBH.SelectedValue = "0";
            this.ddlGYSID.SelectedValue = "0";
            this.ddlLXBH.SelectedValue = "0";
        }


        protected class TableRowItem
        {
            public int Index { get; set; }
            public HtmlTableRow Tr { get; set; }
            public string Key { get; set; }
            public string Value { get; set; }
            public DropDownList Ddl { get; set; }
            public List<CS_WLSXZ> Data { get; set; }
            public string Wllxbh { get; set; }
            public Label Lbl { get; set; }

            public bool Enable { get; set; }

            public TableRowItem(int index, string key, string value, DropDownList ddl, Label label, HtmlTableRow tr, string wllxbh)
            {
                this.Index = index;
                this.Key = key;
                this.Value = value;
                this.Ddl = ddl;
                this.Wllxbh = wllxbh;
                this.Lbl = label;
                this.Tr = tr;
                Init();
            }

            private void Init()
            {
                this.Lbl.Text = Value;

                // 获取属性值
                CS_WLSXZ_Dao daoWLSXZ = new CS_WLSXZ_Dao();
                List<CS_WLSXZ> list = daoWLSXZ.Query(this.Wllxbh, this.Key);
                this.Ddl.Items.Clear();
                if (list.Count > 0)
                {
                    this.Ddl.DataSource = list;
                    this.Ddl.DataTextField = "MC";
                    this.Ddl.DataValueField = "BH";
                    this.Ddl.DataBind();
                    this.Enable = true;
                }
                else
                {
                    this.Enable = false;
                }
                this.Ddl.Items.Insert(0, new ListItem("请选择", "0"));
            }
        }

        protected void ddlLXBH_SelectedIndexChanged(object sender, EventArgs e)
        {
            // 根据类型获取属性
            string type = ddlLXBH.SelectedValue;
            ShowSXDropDownList(type);
        }

        private void ShowSXDropDownList(string type)
        {
            CS_WLLXSX_Dao daoWLLXSX = new CS_WLLXSX_Dao();
            List<CS_WLLXSX> listWLLXSX = daoWLLXSX.Query(type);
            listItems.Clear();
            if (listWLLXSX.Count > 0)
            {
                // 将8个属性下拉框依次初始化
                for (int i = 0; i < listWLLXSX.Count; i++)
                {
                    string key = listWLLXSX[i].BH;
                    string value = listWLLXSX[i].MC;
                    DropDownList ddl = listDropDownList[i];
                    Label lbl = listLabel[i];
                    HtmlTableRow tr = listTableRow[i];
                    TableRowItem tri = new TableRowItem(i, key, value, ddl, lbl, tr, this.ddlLXBH.SelectedValue);
                    listItems.Add(tri);
                }

                // 显示row
                foreach (TableRowItem item in listItems)
                {
                    item.Tr.Visible = item.Enable;
                }

            }
            else
            {
                tr1.Visible = false;
                tr2.Visible = false;
                tr3.Visible = false;
                tr4.Visible = false;
                tr5.Visible = false;
                tr6.Visible = false;
                tr7.Visible = false;
                tr8.Visible = false;
            }
        }

        protected void btnBH_Click(object sender, EventArgs e)
        {
            //           1、编号是统一长度，15位，其中第一位为数字2代表原料，材料类型2位，供应商4位。
            // 前7位是固定的，后面属性个数是变化的（有的材料属性有5个，个别材料属性只有3个），
            // 每个属性随材料类型而变化，对应的字段长定长2位，但编码长度17不变，没有属性的补充0。


            if (ddlLXBH.SelectedValue.Equals("0"))
            {
                this.lbResult.Text = "请选择类型";
                return;
            }
            if (ddlGYSID.SelectedValue.Equals("0"))
            {
                this.lbResult.Text = "请选择供应商";
                return;
            }
            StringBuilder sb = new StringBuilder();
            sb.Append("2");
            string cllx = ddlLXBH.SelectedValue.Substring(0, 2);
            cllx = Utils.StringHelper.GetLengthStringWithChar(cllx, 2, '0');
            sb.Append(cllx);

            string gysbh = "";
            Hashtable ht = Cache.Get(GYS) as Hashtable;
            if ((ht != null && ht.ContainsKey(ddlGYSID.SelectedValue)))
            {
                gysbh = (string)ht[ddlGYSID.SelectedValue];
                gysbh = gysbh.Substring(0, 4);
            }
            gysbh = Utils.StringHelper.GetLengthStringWithChar(gysbh, 4, '0');
            sb.Append(gysbh);

            sb.Append("-");

            for (int i = 0; i < 5; i++)
            {
                if (listDropDownList[i].SelectedValue == "0")
                {
                    sb.Append("00");
                }
                else
                {
                    string s = "";
                    if (listItems.Count > 0 && i <= listItems.Count)
                    {
                        TableRowItem tri = listItems[i];
                        if (tri != null)
                        {
                            //s = tri.Key.Trim();
                            // 
                            DropDownList dl = listDropDownList[i];
                            s = dl.SelectedValue.Trim();
                            s = s.Substring(0, 2);
                        }
                    }

                    s = Utils.StringHelper.GetLengthStringWithChar(s, 2, '0');
                    sb.Append(s);
                }
            }

            //foreach (TableRowItem item in listItems)
            //{
            //    if (item.Enable)
            //    {
            //        int i = item.Index;
            //        if (listDropDownList[i].SelectedValue == "0")
            //            continue;
            //        sb.Append(item.Key.Trim());
            //    }
            //}

            string newBh = sb.ToString();
            this.tbBH.Text = newBh;

            // 检查编号是否已经存在，若存在则提醒
            lbResult.Text = "";
            ZL_WL_Dao dao = new ZL_WL_Dao();
            if (dao.IsExistBH(newBh))
            {
                this.tbBH.Text = "";
                lbResult.Text = "此编号已经存在，请重新选择";
            }
        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            this.lbResult.Text = "";

            if (this.ddlDWBH.SelectedValue == "0")
            {
                this.lbResult.Text = "请选择单位";
                return;
            }
            if (this.ddlLXBH.SelectedValue == "0")
            {
                this.lbResult.Text = "请选择类型";
                return;
            }
            if (this.ddlGYSID.SelectedValue == "0")
            {
                this.lbResult.Text = "请选择供应商";
                return;
            }

            if (this.ddlYSLXBH.SelectedValue == "0")
            {
                this.lbResult.Text = "请选择颜色类型";
                return;
            }

            if (this.ddlYSBH.SelectedValue == "0")
            {
                this.lbResult.Text = "请选择颜色";
                return;
            }

            ZL_WL_Dao dao = new ZL_WL_Dao();

            string newBh = this.tbBH.Text.Trim();
            if (!newBh.Equals(this.hdBh.Value) && dao.IsExistBH(newBh))
            {
                this.tbBH.Text = "";
                lbResult.Text = "此编号已经存在，请重新选择";
                return;
            }

            ZL_WL entity = new ZL_WL()
            {
                ID = Guid.Parse(this.hdId.Value),
                BH = this.tbBH.Text.Trim(),
                MC = this.tbMC.Text.Trim(),
                GG = this.tbGG.Text.Trim(),
                DWBH = this.ddlDWBH.SelectedValue,
                LXBH = this.ddlLXBH.SelectedValue,
                GYSID = Guid.Parse(this.ddlGYSID.SelectedValue),
                YSLXBH = this.ddlYSLXBH.SelectedValue,
                YSBH = this.ddlYSBH.SelectedValue,
                AQKCSL = double.Parse(this.tbAQKCSL.Text.Trim())
            };

            //添加属性
            foreach (TableRowItem item in listItems)
            {
                if (item.Enable)
                {
                    int c = item.Index + 1;
                    switch (c)
                    {
                        case 1:
                            entity.SXBH1 = item.Key;
                            entity.SXMC1 = item.Value;
                            entity.SXZBH1 = DropDownList1.SelectedValue;
                            entity.SXZMC1 = DropDownList1.SelectedItem.Text;
                            break;
                        case 2:
                            entity.SXBH2 = item.Key;
                            entity.SXMC2 = item.Value;
                            entity.SXZBH2 = DropDownList2.SelectedValue;
                            entity.SXZMC2 = DropDownList2.SelectedItem.Text;
                            break;
                        case 3:
                            entity.SXBH3 = item.Key;
                            entity.SXMC3 = item.Value;
                            entity.SXZBH3 = DropDownList3.SelectedValue;
                            entity.SXZMC3 = DropDownList3.SelectedItem.Text;
                            break;
                        case 4:
                            entity.SXBH4 = item.Key;
                            entity.SXMC4 = item.Value;
                            entity.SXZBH4 = DropDownList4.SelectedValue;
                            entity.SXZMC4 = DropDownList4.SelectedItem.Text;
                            break;
                        case 5:
                            entity.SXBH5 = item.Key;
                            entity.SXMC5 = item.Value;
                            entity.SXZBH5 = DropDownList5.SelectedValue;
                            entity.SXZMC5 = DropDownList5.SelectedItem.Text;
                            break;
                        case 6:
                            entity.SXBH6 = item.Key;
                            entity.SXMC6 = item.Value;
                            entity.SXZBH6 = DropDownList7.SelectedValue;
                            entity.SXZMC7 = DropDownList7.SelectedItem.Text;
                            break;
                        case 7:
                            entity.SXBH7 = item.Key;
                            entity.SXMC7 = item.Value;
                            entity.SXZBH7 = DropDownList7.SelectedValue;
                            entity.SXZMC7 = DropDownList7.SelectedItem.Text;
                            break;
                        case 8:
                            entity.SXBH8 = item.Key;
                            entity.SXMC8 = item.Value;
                            entity.SXZBH8 = DropDownList8.SelectedValue;
                            entity.SXZMC8 = DropDownList8.SelectedItem.Text;
                            break;
                    }
                }
            }

            string resultMessage = "";
            try
            {
                int result = dao.Update(entity);

                if (result == 1)
                {
                    resultMessage = "保存成功";
                    ClearControlsValue();
                }
            }
            catch (Exception ex)
            {
                resultMessage = "保存失败" + ex.Message;
            }
            finally
            {
                this.lbResult.Text = resultMessage;
            }
        }


        protected void btnClear_Click(object sender, ImageClickEventArgs e)
        {
            initValue();
        }

        protected void btnReturn_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("ZL_WL_Query.aspx");
        }

        private void InitYS()
        {
            this.ddlYSBH.Items.Clear();
            string yslx = this.ddlYSLXBH.SelectedValue;
            if (!yslx.Equals("0"))
            {
                CS_YS_Dao dao = new CS_YS_Dao();
                List<CS_YS> list = dao.QueryByLx(yslx);
                if (list.Count > 0)
                {
                    this.ddlYSBH.DataSource = list;
                    this.ddlYSBH.DataTextField = "MC";
                    this.ddlYSBH.DataValueField = "BH";
                    this.ddlYSBH.DataBind();
                }
            }

            this.ddlYSBH.Items.Insert(0, new ListItem("请选择", "0"));
        }

        protected void ddlYSLXBH_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitYS();
        }

    }
}